package ru.spb.osll.myfit.client.dialog;

import ru.spb.osll.myfit.client.event.PageEvent;
import ru.spb.osll.myfit.client.service.FitService;
import ru.spb.osll.myfit.client.service.FitServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TextInputDialog extends DialogBox {
  protected PageEvent m_sourceEvent = null;
 
  protected final FitServiceAsync m_fitService = GWT.create(FitService.class);
  private DialogEventListener m_listener;
  
  public TextInputDialog(DialogEventListener listener) {
    m_listener = listener;
    setText("Enter text:");
   

    final TextArea m_textBox = new TextArea();
    m_textBox.setWidth("400px");
    m_textBox.setHeight("400px");
    
    Button m_cbButtonOk = new Button();
    m_cbButtonOk.setText("Add");
    
    
    Button m_cbButtonCancel = new Button();
    m_cbButtonCancel.setText("Cancel");
      
    m_cbButtonCancel.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    
    m_cbButtonOk.addClickHandler(new ClickHandler() {
      
      @Override
      public void onClick(ClickEvent event) {
        hide();
        String text = m_textBox.getText();
        if(!text.isEmpty()){
          hide();
          m_listener.createElement(text, m_sourceEvent);
          m_textBox.setText("");
          m_sourceEvent = null;
        }
      }
    });
    
    HorizontalPanel hPanelButton = new HorizontalPanel();
    hPanelButton.setSpacing(10);
    hPanelButton.add(m_cbButtonOk);
    hPanelButton.add(m_cbButtonCancel);

    VerticalPanel vPanel = new VerticalPanel();
    vPanel.add(m_textBox);
    vPanel.add(hPanelButton);
    this.setWidget(vPanel);
  }
 
  public void setSourceEvent(PageEvent event){
    m_sourceEvent = event;
  }
}
